{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b3fb5b7-03a8-49cf-b2be-cf28384212ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# File: trampoline.ipynb\n",
    "# Code: Claude Code and Codex\n",
    "# Review: Ryoichi Ando (ryoichi.ando@zozo.com)\n",
    "# License: Apache v2.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a14ae57-5fe0-45b2-a17b-2b82620aac19",
   "metadata": {},
   "outputs": [],
   "source": [
    "from frontend import App\n",
    "\n",
    "# create an app\n",
    "app = App.create(\"trampoline\")\n",
    "\n",
    "# create a square sheet mesh\n",
    "V, F = app.mesh.square(res=128, ex=[1, 0, 0], ey=[0, 0, 1])\n",
    "app.asset.add.tri(\"sheet\", V, F)\n",
    "\n",
    "# create an armadillo tetrahedral mesh\n",
    "V, F, T = app.mesh.preset(\"armadillo\").decimate(30000).tetrahedralize().normalize()\n",
    "app.asset.add.tet(\"armadillo\", V, F, T)\n",
    "\n",
    "# create a scene\n",
    "scene = app.scene.create()\n",
    "\n",
    "# add sheet as trampoline with dynamic area coloring\n",
    "sheet = scene.add(\"sheet\").dyn_color(\"area\", 1.0).direction([1, 0, 0], [0, 0, 1])\n",
    "\n",
    "# pin the four edges of the sheet\n",
    "sheet.pin(sheet.grab([-1, 0, 0]) + sheet.grab([1, 0, 0]))\n",
    "sheet.param.set(\"young-mod\", 1e4).set(\"strain-limit\", 0.01)\n",
    "\n",
    "# add armadillo falling onto trampoline with velocity\n",
    "armadillo = scene.add(\"armadillo\").color(0.75, 0.75, 0.75).scale(0.75).rotate(180, \"y\")\n",
    "armadillo.at(0, 1, 0).jitter().velocity(0, -5, 0)\n",
    "armadillo.param.set(\"density\", 6e3).set(\"friction\", 0.5)\n",
    "\n",
    "# add invisible walls to contain the simulation\n",
    "gap = 0.025\n",
    "scene.add.invisible.wall([1 + gap, 0, 0], [-1, 0, 0])\n",
    "scene.add.invisible.wall([-1 - gap, 0, 0], [1, 0, 0])\n",
    "scene.add.invisible.wall([0, 0, 1 + gap], [0, 0, -1])\n",
    "scene.add.invisible.wall([0, 0, -1 - gap], [0, 0, 1])\n",
    "\n",
    "# compile the scene and report stats\n",
    "scene = scene.build().report()\n",
    "\n",
    "# preview the initial scene\n",
    "scene.preview()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec41f95d-bec2-4773-a678-b4c2f7b75a71",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a new session with the compiled scene\n",
    "session = app.session.create(scene)\n",
    "\n",
    "# set session parameters\n",
    "session.param.set(\"frames\", 120)\n",
    "\n",
    "# build this session\n",
    "session = session.build()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2b8afa5-0bea-436b-86bf-e2c1786b04b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# start the simulation and live-preview the results\n",
    "session.start().preview()\n",
    "\n",
    "# also show simulation logs in realtime\n",
    "session.stream()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4b871d7-f804-49e5-9b89-cb7be517d198",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create an animation from the simulation results\n",
    "session.animate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d1c8d6b-b188-45eb-a854-ffbfa268ce03",
   "metadata": {},
   "outputs": [],
   "source": [
    "# export the animation to file\n",
    "session.export.animation()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bd37b71-a4ae-4923-a794-5e524a93d58d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# this is for CI\n",
    "if app.ci:\n",
    "    assert session.finished()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
